<?php


namespace app\engine\controller;


use cmf\controller\HomeBaseController;
use think\Db;

class FilesController extends HomeBaseController
{
    public function index()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            $this->error('缺少关键参数');
        }
        $where['eid'] = ['=', $data['id']];
        $userId = cmf_get_current_admin_id();
        $res = Db::name('file')
            ->alias('f')
            ->join('user u', 'f.userid = u.id')
            ->field(['f.id', 'f.userid', 'f.fileName', 'f.createTime', 'f.path', 'f.eid', 'f.isuse', 'u.user_login userName'])
            ->where($where)
            ->select()
            ->toArray();
        foreach ($res as &$v) {
            $v['createTime'] = date('Y-m-d H:i:s', $v['createTime']);
        }
        $this->assign('userId', $userId);
        $this->assign('res', $res);
        $this->assign('eid', $data['id']);
        return $this->fetch();
    }

    public function uploads()
    {
        $data = $this->request->param();
        $this->assign('eid', $data['eid']);
        return $this->fetch();
    }

    public function doadd()
    {
        $data = $this->request->post();
        if (key_exists('isuse', $data)) {
            $data['isuse'] = 1;
        }
        $file = request()->file('file');

        if (empty($file)) {
            $this->error('请上传文件');
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $path = CMF_ROOT . 'public' . '/' . 'upload' . '/';
            $info = $file->move($path, true);
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                $data['path'] = $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
        $data['createTime'] = time();
        $data['userid'] = cmf_get_current_admin_id();
        $res = Db::name('file')->insert($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function down()
    {
        $data = $this->request->param();
        $chack = Db::name('file')->where($data)->find();
        if (cmf_get_current_admin_id() != $chack['userid'] && $chack['isuse'] != 1) {
            $this->error('无下载权限');
        }
        $newpath = CMF_ROOT . 'public/upload/' . $chack['path'];
        $this->downfile($newpath);
    }

    function downfile($path)
    {
        $filename = realpath($path); //文件名
        $arr = explode("/", $path);
        $last = $arr[count($arr) - 1];
        $file = fopen($filename, "rb");
        Header("Content-type:  application/octet-stream ");
        Header("Accept-Ranges:  bytes ");
        Header("Content-Disposition:  attachment;  filename=$last");
        $contents = "";
        while (!feof($file)) {
            $contents .= fread($file, 8192);
        }
        echo $contents;
        fclose($file);
    }

    public function del()
    {
        $data = $this->request->param();
        $check = Db::name('file')->where(['id' => $data['id']])->find();
        $newpath = CMF_ROOT . 'public/upload/' . $check['path'];
        if (file_exists($newpath)) {
            unlink($newpath);
        }
        $res = Db::name('file')->where(['id' => $data['id']])->delete();
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function open()
    {
        $data = $this->request->param();
        $check = Db::name('file')->where(['id' => $data['id']])->value('isuse') ? 0 : 1;
        $res = Db::name('file')->where(['id' => $data['id']])->update(['isuse' => $check]);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }
}